<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  	$this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  	$this->load->view($this->session->userdata('formain'));
	  	$this->load->view('layouts/footer');
	 }


	public function insert_users(){
	 	$result = $this->users_model->get_bu_id($this->input->post('company'));
	 	$ena = $this->input->post('user_ena');
	 	if($this->input->post('user_type') == 'EXT'){
	 		$e_id = $this->input->post('entity');
	 		$pwd = random_string('alnum', 8);
	 		$pwd2 = $pwd;
	 	}else{
	 		$e_id = NULL;
	 		$pwd = $this->input->post('passwd');
	 		$pwd2 = $pwd;
	 	}
	 	$data = array(
	 			':p_company_id'		=> $this->input->post('company'),
	 			':p_user_name'		=> $this->input->post('user_name'),
	 			':p_first_name'		=> $this->input->post('f_name'),
	 			':p_last_name'		=> $this->input->post('l_name'),
	 			':p_pwd'			=> password_hash($pwd2,PASSWORD_DEFAULT),
	 			':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 			':p_user_id'		=> $this->session->userdata('user_id'),
	 			'p_department'		=> $this->input->post('department'),
	 			':p_email' 			=> $this->input->post('email'),
	 		);
	 	$query = $this->users_model->insert_user($data);
	 	$config['protocol'] = "smtp";
		$config['smtp_host'] = "ssl://smtp.gmail.com";
		$config['mailpath'] = "assets/tmp";
		$config['smtp_port'] = 465;
		$config['smtp_user'] = "tetsuyakuroko101@gmail.com";
		$config['smtp_pass'] = "dwuydqsvumopnxdk";
		$config['smtp_timeout'] = 5;
		$config['charset'] = "utf-8";
		$config['mailtype'] = "html";
		$config['newline'] = "\r\n";
		$config['crlf'] = "\n";
		$this->email->initialize($config);

	 	if($query[0]->error == 1){
	 		echo json_encode($query);
	 	}else{
	 		$data2 = array(
	 			':pl_user_id'	=> $query[0]->id,
	 			':p_bu_id'		=> $result[0]->bu_id,
	 			':p_user_id'	=> $this->session->userdata('user_id')
	 		);
	 		$query2 = $this->users_model->insert_bu_access($data2);
	 		if($this->input->post('user_type') == 'EXT'){
				$msg = 'Dear Customer, </br> Your Password is <b>'.$pwd.'</b>. </br> Please change your password to secure your account. Thank You.';

				$this->email->from('kyogojowatersystem', $data[':p_user_name']);
				//$list = array($this->input->post('email_to'));
				$this->email->to($data[':p_user_name']);
				$this->email->subject('Kyogojo Account Registration');
				$this->email->message($msg);
				if ($this->email->send()) {
					//echo json_encode(array('msg' => 'success'));
				} else {
					//echo json_encode(array('msg' => 'fail'));
					show_error($this->email->print_debugger());
				}
			}
	 		if($query2[0]->error == 1){
	 			echo json_encode($query2);
	 		}else{
	 			echo json_encode($query);
	 		}
	 	}
	 }
	 public function update_users(){
	 	$ena = $this->input->post('e_usr_ena');
	 	$password;
	 	if($this->input->post('n_e_pwd') != ""){
 			$password = password_hash($this->input->post('n_e_pwd'),PASSWORD_DEFAULT);
 			
	 	}else{
	 		$password = $this->input->post('e_pwd');
	 		
	 	}
	 	$data = array(
	 			':pl_user_id'		=> $this->input->post('hidden_user'),
	 			':p_company_id'		=> $this->input->post('e_company_id'),
	 			':p_user_name'		=> $this->input->post('e_usr_name'),
	 			':p_first_name'		=> $this->input->post('e_f_name'),
	 			':p_last_name'		=> $this->input->post('e_l_name'),
	 			':p_pwd'			=> $password,
	 			':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 			':p_user_id'		=> $this->session->userdata('user_id'),
	 			'p_department'		=> $this->input->post('department'),
	 			':p_email'			=> $this->input->post('e_email'),
	 		);
	 	$this->users_model->update_user($data);
	 }
	 public function search_users(){
	 	$uname = $this->input->post('search_u_name');
	 	$fname = $this->input->post('search_f_name');
	 	$lname = $this->input->post('search_l_name');
	 	$query = $this->users_model->search_users($uname,$fname,$lname);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
	 }



	 public function insert_role_access(){
	 	$ena = $this->input->post('usr_ena');
	 	$endDate = $this->input->post('usr_end');
	 	if($this->input->post('usr_end') == '0000-00-00' || $this->input->post('usr_end') == ''){
	 		$endDate = NULL;
	 	}
	 	$data = array(
	 			':pl_user_id'		=> $this->input->post('hide_usr_id'),
	 			':p_role_id'		=> $this->input->post('usr_role'),
	 			':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 			':p_start_date'		=> $this->input->post('usr_start'),
	 			':p_end_date'		=> $endDate,
	 			':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query = $this->users_model->insert_role_access($data);
	 	if($query[0]->error == 1){
	 		echo json_encode($query);
	 	}else{
	 		$query2 = $this->users_model->get_role_access($this->input->post('hide_usr_id'));
	 		echo json_encode($query2);
	 	}

	 }
	 public function update_role_access(){
	 	$endDate = $this->input->post('role_usr_end');
	 	$ena = $this->input->post('role_usr_ena');
	 	if($this->input->post('role_usr_end') == '0000-00-00' || $this->input->post('role_usr_end') == ''){
	 		$endDate = NULL;
	 	}
	 	$data = array(
	 			':pl_user_id'		=> $this->input->post('role_hidden_user_id'),
	 			':p_role_id'		=> $this->input->post('role_usr_id'),
	 			':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),
	 			':p_start_date'		=> $this->input->post('role_usr_start'),
	 			':p_end_date'		=> $endDate,
	 			':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$query = $this->users_model->insert_role_access($data);
	 	if($query[0]->error == 1){
	 		echo json_encode($query);
	 	}else{
	 		$query2 = $this->users_model->get_role_access($this->input->post('role_hidden_user_id'));
	 		echo json_encode($query2);
	 	}

	 }
	 public function update_inline_role_access(){
	 	$endDate = $this->input->post('end');
	 	if($this->input->post('end') == '0000-00-00' || $this->input->post('end') == ''){
	 		$endDate = NULL;
	 	}
	 	$data = array(
	 			':pl_user_id'		=> $this->input->post('user_id'),
	 			':p_role_id'		=> $this->input->post('role_id'),
	 			':p_start_date'		=> $this->input->post('start'),
	 			':p_end_date'		=> $endDate,
	 			':p_user_id'		=> $this->session->userdata('user_id')
	 		);
	 	$this->users_model->update_role_access($data);
	 }




	 public function view_roles_access($id){
	 	$query2 = $this->users_model->get_role_access($id);
	 	echo json_encode($query2);
	 }



	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
				'company'	=> $this->users_model->get_companies(),
				'roles'		=> $this->users_model->get_roles(),
				'entity'	=> $this->users_model->get_entity(),
				'user_type' => $this->users_model->get_user_type(),
				'department'=> $this->wus->get_lookup_values_by_type('DEPARTMENT')
			));
	} 
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
			'company'		=> $this->users_model->get_companies(),
			'roles'			=> $this->users_model->get_roles(),
			'users_data'	=> $this->users_model->get_users_data($id),
			'user_type' 	=> $this->users_model->get_user_type(),
			'entity'		=> $this->users_model->get_entity(),
			'department'	=> $this->wus->get_lookup_values_by_type('DEPARTMENT')
		));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
			'company'		=> $this->users_model->get_companies(),
			'role_access'	=> $this->users_model->get_role_access($id),
			'users_data'	=> $this->users_model->get_users_data($id),
			'department'	=> $this->wus->get_lookup_values_by_type('DEPARTMENT')
		));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */